#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#include "newclient.c"

void init_daemon(FILE *fp, time_t *curtime){
    fp = fopen("daemon.log","a");
    time(curtime);
    
    // if fork() return 0:create a new process,if -1:create error,else ID:father process id.
    pid_t pid, sid;

    pid = fork();
    if(pid < 0){
        fprintf(fp, "ERROR ONE, Sorry christina, fork function create process error, the time is: %s",ctime(curtime));
        exit(EXIT_FAILURE);
    }
    if(pid > 0){
        // exit the father process
        fprintf(fp, "ERROR THREE, Sorry christina, exit father process error, the time is: %s",ctime(curtime));
        exit(EXIT_SUCCESS);
    }
    
    // set mask to 0
    umask(0);
    sid = setsid();
    if(sid < 0){
        fprintf(fp, "ERROR FOUR, Sorry christina, create new SID for process error, the time is: %s",ctime(curtime));
        exit(EXIT_FAILURE);
    }
    if(chdir("/") < 0){
        fprintf(fp, "ERROR TWO, Sorry christina, change dir error, the time is: %s",ctime(curtime));
        perror("change dir error");
        exit(EXIT_FAILURE);
    }

    // close standard file describe symbol
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    // create child process successful
    fprintf(fp, "Congratulations, daemon process successful, the time is: %s, the process id is: %d\n",ctime(curtime),getpid());



    fclose(fp);
}

int main(){
    int count = 10;
    int status, ret;
    FILE *fp;
    time_t curtime;
    char str[2048] = {0};
    int offset = 0;

    init_daemon(fp, &curtime);

    pid_t pid;
    pid = fork();

    if(pid < 0){
        // the failure process fork
        if((fp = fopen("daemon.log","a")) >= 0){
            time(&curtime);
            fprintf(fp,"ERROR ERROR, Sorry christina, failure in child process create, the time is: %s\n",ctime(&curtime));
            offset += sprintf(str+offset, "ERROR ERROR, Sorry christina, failure in child process create, the time is: %s\n",ctime(&curtime));
            fclose(fp);
            exit(EXIT_FAILURE);
        }
    }
    else if(pid > 0){
        // for the parent process
        if((fp = fopen("daemon.log","a")) >= 0){
            time(&curtime);
            fprintf(fp,"Congratulations, christina, i am the parent process: id = %d, the time is: %s\n",getpid(),ctime(&curtime));
            offset += sprintf(str+offset, "Congratulations, christina, i am the parent process: id = %d, the time is: %s\n",getpid(),ctime(&curtime));
            ret = wait(&status);
            fprintf(fp,"res:%d, status=%d\n", ret, status);
            offset += sprintf(str+offset, "res:%d, status=%d\n", ret, status);
            if (WIFEXITED(status)){
                fprintf(fp,"normal termination, exit status = %d\n", WEXITSTATUS(status));
                offset += sprintf(str+offset, "normal termination, exit status = %d\n", WEXITSTATUS(status));
            }
            else if (WIFSIGNALED(status)){
                fprintf(fp,"abnormal termination, signal number = %d\n", WTERMSIG(status));
                offset += sprintf(str+offset, "abnormal termination, signal number = %d\n", WTERMSIG(status));
            }
            else if (WIFSTOPPED(status)){
                fprintf(fp,"child stopped, signal number=%d\n", WSTOPSIG(status));
                offset += sprintf(str+offset, "child stopped, signal number=%d\n", WSTOPSIG(status));
            }
            fclose(fp);
            
            
        }

        system("/home/christina/桌面/deamon/HaChiException &");
    }
    else{
        // for the child process
        if((fp = fopen("daemon.log","a")) >= 0){
            time(&curtime);
            fprintf(fp,"Congratulations, christina, i am the child process: id = %d, my parent process : id = %d, the time is: %s\n",getpid(),getppid(),ctime(&curtime));
            // int response;
            // response = execl("/home/christina/桌面/daemon/hello","hello",NULL);
            fclose(fp);
        }
        
        
    }
    
    initClient(str);

    // NOCHDIR == 0,chdir to /,NOCLOSE == 0,redirect to /dev/null
    // daemon(0,0);
    fp = fopen("daemon.log","a");
    while (1){
        
        // // put current timer into time_t
        // time(&curtime);
        // fprintf(fp,"SUCCESS WAY, Hello christina, the time is: %s",ctime(&curtime));
        
        if((fp = fopen("daemon.log","a")) >= 0){
            // put current timer into time_t
            time(&curtime);
            fprintf(fp,"SUCCESS WAY, Hello christina, the time is: %s, the process id is: %d\n",ctime(&curtime),getpid());
            fclose(fp);
        }

        sleep(10);

        // add new listen code

    }
    fclose(fp);
    return 0;
}